﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GoodTrader.GTOption
{
    public partial class CpOptionCodeTable : Form
    {
        private GTDownload.CpOptionCodeManager mOptManager = null;
        public GTDownload.CpOptionCodeManager OptManager
        {
            get { return mOptManager; }
            set { mOptManager = value; }
        }
        private SourceGrid.Cells.Views.Cell categoryView = null;
        private SourceGrid.Cells.Views.Cell headerView = null;
        private SourceGrid.Cells.Views.Cell priceView = null;
        private SourceGrid.Cells.Views.Cell buyView = null;
        private SourceGrid.Cells.Views.Cell sellView = null;
        private SourceGrid.Cells.Views.Cell centerView = null;
        private SourceGrid.Cells.Views.Cell normalView = null;

        private SourceGrid.Cells.Views.Cell highView = null;
        private SourceGrid.Cells.Views.Cell lowView = null;
        private SourceGrid.Cells.Views.Cell baseView = null;
        private SourceGrid.Cells.Views.Cell curView = null;

        private SourceGrid.Cells.Views.Cell upView = null;
        private SourceGrid.Cells.Views.Cell downView = null;

        public CpOptionCodeTable(GTDownload.CpOptionCodeManager aOptManager)
        {
            mOptManager = aOptManager;
            InitializeComponent();
            InitGridCellView();
            InitGrid();
            MakeTable(aOptManager.CodeArray, aOptManager.PriceCodeList, aOptManager.OptionATMDownloader.ATMArray);
        }

        public void InitGridCellView()
        {
            DevAge.Drawing.RectangleBorder border = new DevAge.Drawing.RectangleBorder(new DevAge.Drawing.BorderLine(Color.DarkGreen), new DevAge.Drawing.BorderLine(Color.DarkGreen));
            //Category header
            //DevAge.Drawing.BorderLine top = new DevAge.Drawing.BorderLine(Color.Blue, 2);
            //DevAge.Drawing.RectangleBorder rcBorder = new DevAge.Drawing.RectangleBorder();

            categoryView = new SourceGrid.Cells.Views.Cell();
            categoryView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.RoyalBlue, Color.LightBlue, 0);
            categoryView.ForeColor = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            categoryView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            categoryView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            categoryView.Font = new Font("굴림", 9, FontStyle.Bold);


            headerView = new SourceGrid.Cells.Views.Cell();
            headerView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.ForestGreen, Color.FloralWhite, 0);
            headerView.ForeColor = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            headerView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            headerView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            headerView.Font = new Font("굴림", 9, FontStyle.Bold);

            priceView = new SourceGrid.Cells.Views.Cell();
            priceView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.ForestGreen, Color.ForestGreen, 0);
            //priceView.ForeColor = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            priceView.ForeColor = Color.White;
            priceView.BackColor = Color.LightSteelBlue;
            priceView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            priceView.Border = DevAge.Drawing.RectangleBorder.RectangleBlack1Width;
            priceView.Font = new Font("굴림", 9, FontStyle.Regular);

            buyView = new SourceGrid.Cells.Views.Cell();
            buyView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.Red, Color.White, 0);
            //priceView.ForeColor = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            buyView.BackColor = Color.LightPink;
            buyView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            //priceView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            buyView.Font = new Font("굴림", 9, FontStyle.Regular);

            sellView = new SourceGrid.Cells.Views.Cell();
            sellView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.Blue, Color.White, 0);
            //priceView.ForeColor = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            sellView.BackColor = Color.LightSteelBlue;
            sellView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            //priceView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            sellView.Font = new Font("굴림", 9, FontStyle.Regular);

            centerView = new SourceGrid.Cells.Views.Cell();
            centerView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.FloralWhite, Color.LightSteelBlue, 90);
            centerView.ForeColor = Color.Red;
            centerView.BackColor = Color.LightSteelBlue;
            centerView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            //priceView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            centerView.Font = new Font("굴림", 9, FontStyle.Regular);

            normalView = new SourceGrid.Cells.Views.Cell();
            //normalView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.ForestGreen, Color.FloralWhite, 0);
            normalView.ForeColor = Color.Black;
            normalView.BackColor = Color.White;
            normalView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            normalView.Font = new Font("굴림", 9, FontStyle.Regular);


            highView = new SourceGrid.Cells.Views.Cell();
            highView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.LightPink, Color.White, 90);
            highView.ForeColor = Color.Black;
            highView.BackColor = Color.White;
            highView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            highView.Font = new Font("굴림", 9, FontStyle.Regular);

            lowView = new SourceGrid.Cells.Views.Cell();
            lowView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.LightBlue, Color.White, 90);
            lowView.ForeColor = Color.Black;
            lowView.BackColor = Color.White;
            lowView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            //lowView.Border. = DevAge.Drawing.BorderLine;
            lowView.Font = new Font("굴림", 9, FontStyle.Regular);


            baseView = new SourceGrid.Cells.Views.Cell();
            baseView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.LightGreen, Color.White, 90);
            baseView.ForeColor = Color.Red;
            baseView.BackColor = Color.White;
            baseView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            baseView.Font = new Font("굴림", 9, FontStyle.Bold);

            curView = new SourceGrid.Cells.Views.Cell();
            //curView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.White, Color.White, 90);
            curView.ForeColor = Color.Black;
            curView.BackColor = Color.White;
            curView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            curView.Font = new Font("굴림", 9, FontStyle.Regular);


            upView = new SourceGrid.Cells.Views.Cell();
            upView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.LightGreen, Color.White, 90);
            upView.ForeColor = Color.Red;
            upView.BackColor = Color.White;
            upView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            upView.Font = new Font("굴림", 9, FontStyle.Regular);

            downView = new SourceGrid.Cells.Views.Cell();
            downView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.LightGreen, Color.White, 90);
            downView.ForeColor = Color.Blue;
            downView.BackColor = Color.White;
            downView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            downView.Font = new Font("굴림", 9, FontStyle.Regular);
        }

        private int mPriceCellWidth = 70;
        private int mMonthCellWidth = 12*8;
        private int mCellRowHeight = 30;

        public void InitGrid()
        {
            gridOptionTable.Redim(30, 15);
            gridOptionTable.FixedRows = 2;
            gridOptionTable.Rows[0].Height = mCellRowHeight;

            gridOptionTable[0, 0] = new SourceGrid.Cells.Cell("콜옵션");
            gridOptionTable[0, 0].SetSpan(1, 4);
            gridOptionTable[0, 0].View = buyView;
            gridOptionTable[0, 4] = new SourceGrid.Cells.Cell("행사가격");
            gridOptionTable[0, 4].SetSpan(2, 1);
            gridOptionTable[0, 4].View = headerView;
            gridOptionTable[0, 4].Column.Width = mPriceCellWidth;
            

            gridOptionTable[0, 5] = new SourceGrid.Cells.Cell("풋옵션");
            gridOptionTable[0, 5].SetSpan(1, 4);
            gridOptionTable[0, 5].View = sellView;

            gridOptionTable[1, 0] = new SourceGrid.Cells.Cell("차차차월");
            gridOptionTable[1, 0].View = buyView;
            gridOptionTable[1, 0].Column.Width = mMonthCellWidth;
            gridOptionTable[1, 1] = new SourceGrid.Cells.Cell("차차월");
            gridOptionTable[1, 1].View = buyView;
            gridOptionTable[1, 1].Column.Width = mMonthCellWidth;
            gridOptionTable[1, 2] = new SourceGrid.Cells.Cell("차월");
            gridOptionTable[1, 2].View = buyView;
            gridOptionTable[1, 2].Column.Width = mMonthCellWidth;
            gridOptionTable[1, 3] = new SourceGrid.Cells.Cell("근월");
            gridOptionTable[1, 3].View = buyView;
            gridOptionTable[1, 3].Column.Width = mMonthCellWidth;
            
            gridOptionTable[1, 5] = new SourceGrid.Cells.Cell("근월");
            gridOptionTable[1, 5].View = sellView;
            gridOptionTable[1, 5].Column.Width = mMonthCellWidth;
            gridOptionTable[1, 6] = new SourceGrid.Cells.Cell("차월");
            gridOptionTable[1, 6].View = sellView;
            gridOptionTable[1, 6].Column.Width = mMonthCellWidth;
            gridOptionTable[1, 7] = new SourceGrid.Cells.Cell("차차월");
            gridOptionTable[1, 7].View = sellView;
            gridOptionTable[1, 7].Column.Width = mMonthCellWidth;
            gridOptionTable[1, 8] = new SourceGrid.Cells.Cell("차차차월");
            gridOptionTable[1, 8].View = sellView;
            gridOptionTable[1, 8].Column.Width = mMonthCellWidth;
        }

        private void MakePriceCell(List<string> aPriceList)
        {
            int fixedRowCount = gridOptionTable.FixedRows;
            for (int i = 0; i < aPriceList.Count; i++)
            {
                gridOptionTable[i + fixedRowCount, 4] = new SourceGrid.Cells.Cell();
                gridOptionTable[i + fixedRowCount, 4].View = centerView;
                gridOptionTable[i + fixedRowCount, 4].Column.Width = mMonthCellWidth;
                gridOptionTable[i + fixedRowCount, 4].Value = aPriceList[i];
            }

            for (int i = 0; i < aPriceList.Count; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    gridOptionTable[i + fixedRowCount, j] = new SourceGrid.Cells.Cell();
                    if (j == 3)
                        gridOptionTable[i + fixedRowCount, j].View = highView;
                    else if (j == 2)
                        gridOptionTable[i + fixedRowCount, j].View = lowView;
                    else if (j == 1)
                        gridOptionTable[i + fixedRowCount, j].View = baseView;
                    else
                        gridOptionTable[i + fixedRowCount, j].View = normalView;
                    gridOptionTable[i + fixedRowCount, j].Column.Width = mMonthCellWidth;
                }
            }

            for (int i = 0; i < aPriceList.Count; i++)
            {
                for (int j = 5; j < 9; j++)
                {
                    gridOptionTable[i + fixedRowCount, j] = new SourceGrid.Cells.Cell();
                    if (j == 5)
                        gridOptionTable[i + fixedRowCount, j].View = highView;
                    else if (j == 6)
                        gridOptionTable[i + fixedRowCount, j].View = lowView;
                    else if (j == 7)
                        gridOptionTable[i + fixedRowCount, j].View = baseView;
                    else
                        gridOptionTable[i + fixedRowCount, j].View = normalView;
                    gridOptionTable[i + fixedRowCount, j].Column.Width = mMonthCellWidth;
                }
            }
        }

        public void MakeTable(string[] aCodeArray, List<string> aPriceList, string[] aATMArray)
        {
            MakePriceCell(aPriceList);
            int fixedRowCount = gridOptionTable.FixedRows;
            int callMonth = 4;
            int putMonth = 4;
            string preMonth = "-1";
            int callATM = -1;
            int putATM = 3;
            for (int i = 0; i < aCodeArray.Length; i++)
            {
                string opCode = aCodeArray[i];
                string sideCode = opCode.Substring(0, 1);
                string monthCode = opCode.Substring(opCode.Length - 4, 1);
                string priceCode = opCode.Substring(opCode.Length - 3, 3);
                int index = mOptManager.FindPriceIndex(priceCode) + fixedRowCount;
                if (sideCode == "2")
                {
                    if (monthCode != preMonth)
                    {
                        callMonth--;
                        gridOptionTable[1, callMonth].Value = monthCode + "월";
                        callATM++;
                    }

                    gridOptionTable[index, callMonth].Value = opCode;

                    if (aATMArray[callATM] == opCode)
                    {
                        gridOptionTable[index, callMonth].View = priceView;
                        gridOptionTable[index, 4].View = priceView;
                    }

                    preMonth = monthCode;
                }
                else
                {
                    if (monthCode != preMonth)
                    {
                        putMonth++;
                        gridOptionTable[1, putMonth].Value = monthCode + "월";
                        putATM++;
                    }

                    gridOptionTable[index, putMonth].Value = opCode;
                    if (aATMArray[putATM] == opCode)
                    {
                        gridOptionTable[index, putMonth].View = priceView;
                    }

                    preMonth = monthCode;
                }
            }
        }
    }
}
